package tp.pr4.commands;

import java.util.StringTokenizer;

import tp.pr4.Game;
import tp.pr4.MensajesConst;
import tp.pr4.Player;
import tp.pr4.commands.exceptions.CommandExecutionException;
import tp.pr4.commands.exceptions.WrongCommandFormatException;
import tp.pr4.items.Item;

public class UseCommand implements Command
{

	private Player _friki;
	private Game _game;
	private String _itemToBeUsedId;

	@Override public void execute() throws CommandExecutionException
	{

		Item item = this._friki.getItem(this._itemToBeUsedId);
		if (item == null)
			throw new CommandExecutionException(MensajesConst.stoledItem+ this._itemToBeUsedId + ".");

		item.use(this._friki, this._game.getCurrentRoom());

	}

	@Override public String getHelp()
	{
		return MensajesConst.hUse;
	}

	@Override public Command parse(String cad, Game execContext)
			throws WrongCommandFormatException
	{
		StringTokenizer tkn = new StringTokenizer(cad);
		if (tkn.countTokens() != 2)
			throw new WrongCommandFormatException();
		String verb = tkn.nextToken();

		if (!(verb.toUpperCase().equalsIgnoreCase("USE") || verb.toUpperCase().equalsIgnoreCase("USAR")))
			throw new WrongCommandFormatException();
		this._game = execContext;
		this._friki = this._game.getPlayer();
		this._itemToBeUsedId = tkn.nextToken();
		return this;

	}

}
